Always parse GTK/GDK/GSK_DEBUG env vars and make some entries available in non-debug...
authorChristoph Reiter <reiter.christoph@gmail.com>
Sat, 14 Nov 2020 07:49:17 +0000 (08:49 +0100)
committerChristoph Reiter <reiter.christoph@gmail.com>
Sun, 15 Nov 2020 10:34:54 +0000 (11:34 +0100)
Currently GTK can be built with G_ENABLE_DEBUG which enables various debug code and parsing
of those env vars, or without, which instead of parsing them prints a warning if they are set.
While building with G_ENABLE_DEBUG isn't strictly needed it's the only way to make GTK_DEBUG=interactive work,
which is a nice thing to have always.

This enables parsing of those env vars in any case and allows specific values being marked as also
available when not built with G_ENABLE_DEBUG (interactive for example). If not built with G_ENABLE_DEBUG
then all unavailable values will be marked as such in the help output and a note is added that
GTK needs to be built with G_ENABLE_DEBUG to use them, which should help discoverability.

docs/reference/gtk/running.md
gdk/gdk-private.h
gdk/gdk.c
gsk/gskdebug.c
gtk/gtkmain.c

index 2b338bd14871fd4d8142ea28dcd2c45490f9bf7d..d294f8fc82a0a3511b24ef3c2bcf7f3242e39e57 100644 (file)
@@ -11,9 +11,9 @@ environment variables.
 
 ### GTK_DEBUG {#GTK_Debug-Options}
 
-Unless GTK has been configured with `-Ddebug=false`, this variable
-can be set to a list of debug options, which cause GTK to print out
-different types of debugging information.
+This variable can be set to a list of debug options, which cause GTK to
+print out different types of debugging information. Some of these options
+are only available when GTK has been configured with `-Ddebug=true`.
 
 actions
  : Actions and menu models
@@ -141,9 +141,9 @@ The `loaders.cache` file is generated by the
 
 ### GDK_DEBUG
 
-Unless GTK has been configured with `-Ddebug=false`, this variable
-can be set to a list of debug options, which cause GDK to print out
-different types of debugging information.
+This variable can be set to a list of debug options, which cause GDK to
+print out different types of debugging information. Some of these options
+are only available when GTK has been configured with `-Ddebug=true`.
 
 cursor
  : Information about cursor objects (only win32)
@@ -191,10 +191,9 @@ to obtain a list of all supported debug options.
 
 ### GSK_DEBUG {#GSK-Debug-Options}
 
-Unless GTK has been configured with `-Ddebug=false`,
-this variable can be set to a list of debug options,
-which cause GSK to print out different types of debugging
-information.
+This variable can be set to a list of debug options, which cause GSK to
+print out different types of debugging information. Some of these options
+are only available when GTK has been configured with `-Ddebug=true`.
 
 renderer
  : General renderer information
index fb058c0f399c4c0caaadd31388827fd7d339a33b..d2d185d57eb3a326f243323551707f6ae80d33fc 100644 (file)
@@ -45,6 +45,7 @@ typedef struct
   const char *key;
   guint value;
   const char *help;
+  gboolean always_enabled;
 } GdkDebugKey;
 
 guint gdk_parse_debug_var (const char        *variable,
index 874e9938929f5a02db7239316d28e06335edb946..953079cdd54c4f62034e676015e1724e1d4404d4 100644 (file)
--- a/gdk/gdk.c
+++ b/gdk/gdk.c
@@ -128,7 +128,6 @@ static int gdk_initialized = 0;                     /* 1 if the library is initi
                                                      * 0 otherwise.
                                                      */
 
-#ifdef G_ENABLE_DEBUG
 static const GdkDebugKey gdk_debug_keys[] = {
   { "misc",            GDK_DEBUG_MISC, "Miscellaneous information" },
   { "events",          GDK_DEBUG_EVENTS, "Information about events" },
@@ -152,7 +151,6 @@ static const GdkDebugKey gdk_debug_keys[] = {
   { "vulkan-validate", GDK_DEBUG_VULKAN_VALIDATE, "Load the Vulkan validation layer" },
   { "default-settings",GDK_DEBUG_DEFAULT_SETTINGS, "Force default values for xsettings" },
 };
-#endif
 
 
 #ifdef G_HAS_CONSTRUCTORS
@@ -212,6 +210,13 @@ gdk_parse_debug_var (const char        *variable,
   const char *q;
   gboolean invert;
   gboolean help;
+  gboolean debug_enabled;
+
+#ifdef G_ENABLE_DEBUG
+  debug_enabled = TRUE;
+#else
+  debug_enabled = FALSE;
+#endif
 
   string = g_getenv (variable);
   if (string == NULL)
@@ -237,21 +242,25 @@ gdk_parse_debug_var (const char        *variable,
         }
       else
         {
+          char *val = g_strndup (p, q - p);
           for (i = 0; i < nkeys; i++)
             {
               if (strlen (keys[i].key) == q - p &&
                   g_ascii_strncasecmp (keys[i].key, p, q - p) == 0)
                 {
+                  if (!debug_enabled && !keys[i].always_enabled)
+                    {
+                      fprintf (stderr, "\"%s\" is only available when building GTK with G_ENABLE_DEBUG. See %s=help\n",
+                               val, variable);
+                      break;
+                    }
                   result |= keys[i].value;
                   break;
                 }
             }
           if (i == nkeys)
-            {
-              char *val = g_strndup (p, q - p);
-              fprintf (stderr, "Unrecognized value \"%s\". Try %s=help\n", val, variable);
-              g_free (val);
-            }
+            fprintf (stderr, "Unrecognized value \"%s\". Try %s=help\n", val, variable);
+          g_free (val);
          }
 
       p = q;
@@ -267,11 +276,17 @@ gdk_parse_debug_var (const char        *variable,
       max_width += 4;
 
       fprintf (stderr, "Supported %s values:\n", variable);
-      for (i = 0; i < nkeys; i++)
-        fprintf (stderr, "  %s%*s%s\n", keys[i].key, (int)(max_width - strlen (keys[i].key)), " ", keys[i].help);
+      for (i = 0; i < nkeys; i++) {
+        fprintf (stderr, "  %s%*s%s", keys[i].key, (int)(max_width - strlen (keys[i].key)), " ", keys[i].help);
+        if (!debug_enabled && !keys[i].always_enabled)
+          fprintf (stderr, " [unavailable]");
+        fprintf (stderr, "\n");
+      }
       fprintf (stderr, "  %s%*s%s\n", "all", max_width - 3, " ", "Enable all values");
       fprintf (stderr, "  %s%*s%s\n", "help", max_width - 4, " ", "Print this help");
       fprintf (stderr, "\nMultiple values can be given, separated by : or space.\n");
+      if (!debug_enabled)
+        fprintf (stderr, "Values marked as [unavailable] are only accessible if GTK is built with G_ENABLE_DEBUG.\n");
     }
 
   if (invert)
@@ -279,7 +294,10 @@ gdk_parse_debug_var (const char        *variable,
       guint all_flags = 0;
 
       for (i = 0; i < nkeys; i++)
-        all_flags |= keys[i].value;
+        {
+          if (debug_enabled || keys[i].always_enabled)
+            all_flags |= keys[i].value;
+        }
 
       result = all_flags & (~result);
     }
@@ -294,14 +312,9 @@ gdk_pre_parse (void)
 
   gdk_ensure_resources ();
 
-#ifdef G_ENABLE_DEBUG
   _gdk_debug_flags = gdk_parse_debug_var ("GDK_DEBUG",
                                           gdk_debug_keys,
                                           G_N_ELEMENTS (gdk_debug_keys));
-#else
-  if (g_getenv ("GDK_DEBUG"))
-    g_warning ("GDK_DEBUG set but ignored because GTK isn't built with G_ENABLE_DEBUG");
-#endif  /* G_ENABLE_DEBUG */
 
 #ifndef G_HAS_CONSTRUCTORS
   stash_desktop_startup_notification_id ();
index 3538cec0e5093ba0e8a744f4f4d3955d68746c9f..4e197257ef1ae7ecde4f498a604ccbda7f567c73 100644 (file)
@@ -1,7 +1,6 @@
 #include "gskdebugprivate.h"
 #include "gdk/gdk-private.h"
 
-#ifdef G_ENABLE_DEBUG
 static const GdkDebugKey gsk_debug_keys[] = {
   { "renderer", GSK_DEBUG_RENDERER, "General renderer information" },
   { "cairo", GSK_DEBUG_CAIRO, "Cairo renderer information" },
@@ -17,14 +16,12 @@ static const GdkDebugKey gsk_debug_keys[] = {
   { "vulkan-staging-image", GSK_DEBUG_VULKAN_STAGING_IMAGE, "Use a staging image for Vulkan texture upload" },
   { "vulkan-staging-buffer", GSK_DEBUG_VULKAN_STAGING_BUFFER, "Use a staging buffer for Vulkan texture upload" }
 };
-#endif
 
 static guint gsk_debug_flags;
 
 static void
 init_debug_flags (void)
 {
-#ifdef G_ENABLE_DEBUG
   static volatile gsize gsk_debug_flags__set;
 
   if (g_once_init_enter (&gsk_debug_flags__set))
@@ -35,7 +32,6 @@ init_debug_flags (void)
 
       g_once_init_leave (&gsk_debug_flags__set, TRUE);
     }
-#endif
 }
 
 gboolean
index 6f7ca2337cfef520375b2368ada56ff27724cf53..8b246477bea904f1c07079b2be7d54fb1949dccf 100644 (file)
@@ -244,7 +244,6 @@ gtk_simulate_touchscreen (void)
   return (gtk_get_debug_flags () & GTK_DEBUG_TOUCHSCREEN) != 0;
 }
 
-#ifdef G_ENABLE_DEBUG
 static const GdkDebugKey gtk_debug_keys[] = {
   { "keybindings", GTK_DEBUG_KEYBINDINGS, "Information about keyboard shortcuts" },
   { "modules", GTK_DEBUG_MODULES, "Information about modules and extensions" },
@@ -260,12 +259,11 @@ static const GdkDebugKey gtk_debug_keys[] = {
   { "builder", GTK_DEBUG_BUILDER, "Trace GtkBuilder operation" },
   { "builder-objects", GTK_DEBUG_BUILDER_OBJECTS, "Log unused GtkBuilder objects" },
   { "no-css-cache", GTK_DEBUG_NO_CSS_CACHE, "Disable style property cache" },
-  { "interactive", GTK_DEBUG_INTERACTIVE, "Enable the GTK inspector" },
+  { "interactive", GTK_DEBUG_INTERACTIVE, "Enable the GTK inspector", TRUE },
   { "touchscreen", GTK_DEBUG_TOUCHSCREEN, "Pretend the pointer is a touchscreen" },
   { "snapshot", GTK_DEBUG_SNAPSHOT, "Generate debug render nodes" },
   { "accessibility", GTK_DEBUG_A11Y, "Information about accessibility state changes" },
 };
-#endif /* G_ENABLE_DEBUG */
 
 /* This checks to see if the process is running suid or sgid
  * at the current time. If so, we don’t allow GTK to be initialized.
@@ -546,15 +544,10 @@ do_pre_parse_initialization (void)
 
   gdk_pre_parse ();
 
-#ifdef G_ENABLE_DEBUG
   debug_flags[0].flags = gdk_parse_debug_var ("GTK_DEBUG",
                                               gtk_debug_keys,
                                               G_N_ELEMENTS (gtk_debug_keys));
   any_display_debug_flags_set = debug_flags[0].flags > 0;
-#else
-  if (g_getenv ("GTK_DEBUG"))
-    g_warning ("GTK_DEBUG set but ignored because GTK isn't built with G_ENABLE_DEBUG");
-#endif  /* G_ENABLE_DEBUG */
 
   env_string = g_getenv ("GTK_SLOWDOWN");
   if (env_string)